C++标准模板(STL)

您所在的位置:网站首页 stl 迭代器类型 C++标准模板(STL)

C++标准模板(STL)

2024-07-13 00:12| 来源: 网络整理| 查看: 265

迭代器库-迭代器原语

迭代器库提供了五种迭代器的定义,同时还提供了迭代器特征、适配器及相关的工具函数。

迭代器分类

迭代器共有五 (C++17 前)六 (C++17 起)种:遗留输入迭代器 (LegacyInputIterator) 、遗留输出迭代器 (LegacyOutputIterator) 、遗留向前迭代器 (LegacyForwardIterator) 、遗留双向迭代器 (LegacyBidirectionalIterator) 、遗留随机访问迭代器 (LegacyRandomAccessIterator) ,及 遗留连续迭代器 (LegacyContiguousIterator) (C++17 起)。

迭代器的分类的依据并不是迭代器的类型,而是迭代器所支持的操作。换句话说,某个类型只要支持相应的操作,就可以作为迭代器使用。例如,完整对象类型指针支持所有遗留随机访问迭代器 (LegacyRandomAccessIterator) 要求的操作,于是任何需要遗留随机访问迭代器 (LegacyRandomAccessIterator) 的地方都可以使用指针。

迭代器的所有类别(除了遗留输出迭代器 (LegacyOutputIterator) 和遗留连续迭代器 (LegacyContiguousIterator) )能组织到层级中,其中更强力的迭代器类别(如遗留随机访问迭代器 (LegacyRandomAccessIterator) )支持较不强力的类别(例如遗留输入迭代器 (LegacyInputIterator) )的所有操作。若迭代器落入这些类别之一且亦满足遗留输出迭代器 (LegacyOutputIterator) 的要求,则称之为可变 迭代器并且支持输入还有输出。称非可变迭代器为常迭代器。

创建拥有从实参推出的类型的 std::move_iterator std::make_move_iterator

template< class Iterator >std::move_iterator make_move_iterator( const Iterator& i );

(C++11 起) (C++14 前)

template< class Iterator >std::move_iterator make_move_iterator( Iterator i );

(C++14 起) (C++17 前)

template< class Iterator > constexpr std::move_iterator make_move_iterator( Iterator i );

(C++17 起)

make_move_iterator 是对给定迭代器 i 构造类型从参数类型推出的 std::move_iterator 的便利函数模板。

参数 i-转换成移动迭代器的输入迭代器 返回值

可用于从通过 i 访问的元素移动的 std::move_iterator

可能的实现 template< class Iterator > std::move_iterator make_move_iterator( Iterator i ) { return std::move_iterator(i); } 调用示例 #include #include #include #include #include #include #include #include struct Cell { int x; int y; Cell() = default; Cell(int a, int b): x(a), y(b) {} Cell(const Cell &cell) { x = cell.x; y = cell.y; } Cell(Cell &cell) { x = cell.x; y = cell.y; cell.x = 0; cell.y = 0; } Cell &operator +=(const Cell &cell) { x += cell.x; y += cell.y; return *this; } Cell &operator +(const Cell &cell) { x += cell.x; y += cell.y; return *this; } Cell &operator *(const Cell &cell) { x *= cell.x; y *= cell.y; return *this; } Cell &operator ++() { x += 1; y += 1; return *this; } bool operator


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3